package com.burgeon.backend.EnterpriseWeChat.rest.vip;

import com.burgeon.backend.EnterpriseWeChat.entity.RFIMVipCount;
import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindVipItemByVipId extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindVipItemByVipId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Vip getData(Vip vip) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(vip.getCardNo()), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception();
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        if(jsonArray.length()>0){
            vip.setTotAmtActual(jsonArray.getJSONArray(0).getString(0));
            vip.setLastDate(jsonArray.getJSONArray(0).getString(1));
            vip.setLastAmtActual(jsonArray.getJSONArray(0).getString(2));
        }
        return vip;
    }

    private MultiValueMap Data(String vipCardNo) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("TOT_AMT_ACTUAL");//	累计购物金额
        colums.add("LASTDATE");//最近购物日
        colums.add("LAST_AMT_ACTUAL");//最近购物金额

        params.put("columns", colums);
        params.put("table", "FA_VIPACC");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'C_VIP_ID;CARDNO'");
        paramss.put("condition", vipCardNo);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindVipItemByVipId g = new FindVipItemByVipId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
//            System.err.println(g.getData("15097507766"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
